package org.jtb.mensawotd;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.util.Log;

class MensaWotdParser {
	private static class Parser {
		private List<String> results = new ArrayList<String>();

		Parser(Pattern p, String source) {
			parse(p, source);
		}

		private void parse(Pattern p, String source) {
			if (source == null) {
				return;
			}

			Matcher m = p.matcher(source);
			if (m.find()) {
				for (int i = 1; i < m.groupCount() + 1; i++) {
					String s = m.group(i).trim();
					//s = Cleaner.clean(s);
					// Log.d("mensawotd", "parser, result: " + s);
					results.add(s);
				}
			}
			if (results.size() == 0) {
				results.add("?");
			}
		}

		List<String> getResults() {
			return Collections.unmodifiableList(results);
		}
	}

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
			"MMM d, yyyy");

	private static final Pattern WORD_PATTERN = Pattern
			.compile("<h3>\\s+(.*)\\s+</h3>");
	private static final Pattern ARTICLE_PATTERN = Pattern.compile(
			"<div[^>]*?>MEANING:</div>.*?<i>(.+?)</i?.*?</div><br>",
			Pattern.DOTALL);
	// private static final Pattern MEANING_PATTERN = Pattern.compile(
	// "<div[^>]*?>MEANING:</div>.*?</i>:\\s*(.+?)\\r?\\n.*?</div><br>",
	// Pattern.DOTALL);
	private static final Pattern MEANING_PATTERN = Pattern.compile(
			"\\r?\\n.+?(<i>.+?)\\s+</div><br>",
			Pattern.DOTALL);
	private static final Pattern ETYMOLOGY_PATTERN = Pattern
			.compile(
					"<div[^>]*?>ETYMOLOGY:</div>\\s*<div style=\"margin-left: 20px;\">\\s*(.+?)</div><br>",
					Pattern.DOTALL);
	private static final Pattern USAGE_PATTERN = Pattern.compile(
			"<div[^>]*?>USAGE:</div>\\s*<div style=\"margin-left: 20px;\">\\s*(.+?)</div><br>",
			Pattern.DOTALL);
	private static final Pattern PRONUNCIATION_PATTERN = Pattern.compile(
			"<div[^>]*?>PRONUNCIATION:</div>.*?(\\(.+?\\)).*?</div><br>",
			Pattern.DOTALL);
	private static final Pattern URL_PATTERN = Pattern
			.compile("<a href=\"http://www\\.facebook\\.com/share\\.php\\?u=(http://wordsmith\\.org/words/.*\\.html)\">");
	private static final Pattern MP3_PATTERN = Pattern
			.compile("<a href=\"(http://wordsmith\\.org/words/.+.mp3)\">");
	private static final Pattern DATE_PATTERN = Pattern
			.compile("<div style=\"color:#AAAAAA; text-align:right; font-size:11px;\">(.+)</div>");

	private String word = null;
	private String pronunciation = null;
	private String meaning = null;
	private String etymology = null;
	private String article = null;
	private String usage = null;
	private String url = null;
	private String mp3 = null;
	private Date date;

	MensaWotdParser(String content) {
		Parser parser;

		parser = new Parser(WORD_PATTERN, content);
		if (parser.getResults().size() == 0) {
			return;
		}
		word = parser.getResults().get(0);
		if (word == null) {
			return;
		}

		parser = new Parser(PRONUNCIATION_PATTERN, content);
		pronunciation = parser.getResults().get(0);

		parser = new Parser(ARTICLE_PATTERN, content);
		article = parser.getResults().get(0);

		parser = new Parser(MEANING_PATTERN, content);
		meaning = parser.getResults().get(0);

		parser = new Parser(ETYMOLOGY_PATTERN, content);
		etymology = parser.getResults().get(0);

		parser = new Parser(USAGE_PATTERN, content);
		usage = parser.getResults().get(0);

		parser = new Parser(URL_PATTERN, content);
		url = parser.getResults().get(0);

		parser = new Parser(MP3_PATTERN, content);
		mp3 = parser.getResults().get(0);

		parser = new Parser(DATE_PATTERN, content);
		String dateString = parser.getResults().get(0);
		try {
			date = DATE_FORMAT.parse(dateString);
		} catch (ParseException e) {
			Log.e("mensawotd", "could not parse date from page: " + dateString,
					e);
			date = new Date();
		}
	}

	String getWord() {
		return word;
	}

	String getMeaning() {
		return meaning;
	}

	String getEtymology() {
		return etymology;
	}

	String getUsage() {
		return usage;
	}

	String getUrl() {
		return url;
	}

	String getPronunciation() {
		return pronunciation;
	}

	String getMp3() {
		return mp3;
	}

	Date getDate() {
		return date;
	}
}
